package proxy.impl;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;

import proxy.ServiceProxy;
import vo.ServiceResponse;
import vo.ValueObject;

public class ServiceProxyImpl implements ServiceProxy {
	private static final Log logger = LogFactory.getLog(ServiceProxyImpl.class);

	public ServiceResponse doService(ValueObject vo, String serviceId, String methodName) {
		ServiceResponse response = new ServiceResponse();
		Object bean = null;
		try {
			bean = util.ApplicationContextUtil.getBean(serviceId);
		} catch (BeansException e) {
			logger.error(e);
			response.setException(e);
			return response;
		}
		try {
			Method method = bean.getClass().getDeclaredMethod(methodName, new Class[] { ValueObject.class });
			Object object = method.invoke(bean, vo);
			response.setResult(object);
			return response;
		} catch (Exception e) {
			response.setException(e);
			e.printStackTrace();
			return response;
		}

	}
}
